package cn.edu.lcu.cs.javateaching.gui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JToolBar;
import java.awt.BorderLayout;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.ImageIcon;
import javax.swing.KeyStroke;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.awt.event.InputEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.FlowLayout;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.text.BadLocationException;
import javax.swing.JTextField;
import java.awt.event.InputMethodListener;
import java.awt.event.InputMethodEvent;

public class Notepad {
	private final static String DEFAULT_FILE_NAME = "未命名.txt";

	private JFrame frame;
	/**
	 * @wbp.nonvisual location=127,277
	 */
	private final JPanel statusBar = new JPanel();
	private File currentFile;

	private JTextArea editArea;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Notepad window = new Notepad();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Notepad() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setTitle("记事本");
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);

		JMenu fileMenu = new JMenu("文件F");
		fileMenu.setMnemonic('F');
		menuBar.add(fileMenu);

		JMenuItem fileNewMenu = new JMenuItem("新建");
		fileNewMenu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		fileNewMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK));
		fileNewMenu
				.setIcon(new ImageIcon(Notepad.class.getResource("/com/sun/java/swing/plaf/windows/icons/File.gif")));
		fileMenu.add(fileNewMenu);

		JMenuItem fileOpenMenu = new JMenuItem("打开");
		fileOpenMenu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser fileChooser = new JFileChooser();
				fileChooser.setFileFilter(new FileNameExtensionFilter("文本文件", "txt"));
				if (fileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
					currentFile = fileChooser.getSelectedFile();
					try {
						Files.lines(Paths.get(currentFile.getAbsolutePath()))
								.forEach(line -> editArea.append(line + System.lineSeparator()));
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			}
		});
		fileOpenMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));
		fileMenu.add(fileOpenMenu);

		JMenuItem fileSaveMenu = new JMenuItem("保存");
		fileSaveMenu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					Files.write(Paths.get(currentFile.getAbsolutePath()), editArea.getText().getBytes());
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		fileSaveMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
		fileMenu.add(fileSaveMenu);

		JMenu editMenu = new JMenu("编辑E");
		editMenu.setMnemonic('E');
		menuBar.add(editMenu);

		JMenu formMenu = new JMenu("格式O");
		formMenu.setMnemonic('O');
		menuBar.add(formMenu);

		JMenu viewMenu = new JMenu("查看V");
		viewMenu.setMnemonic('V');
		menuBar.add(viewMenu);

		JMenu helpMemu = new JMenu("帮助H");
		helpMemu.setMnemonic('H');
		menuBar.add(helpMemu);

		JToolBar toolBar = new JToolBar();
		frame.getContentPane().add(toolBar, BorderLayout.NORTH);
		FlowLayout flowLayout = (FlowLayout) statusBar.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		frame.getContentPane().add(statusBar, BorderLayout.SOUTH);

		JLabel statusWordNumber = new JLabel("0 字");
		statusBar.add(statusWordNumber);

		JSeparator separator = new JSeparator();
		separator.setOrientation(SwingConstants.VERTICAL);
		statusBar.add(separator);

		JLabel statusCoordinate = new JLabel("0,0");
		statusBar.add(statusCoordinate);

		JButton fileNewBtn = new JButton("");
		fileNewBtn.setIcon(new ImageIcon(Notepad.class.getResource("/com/sun/java/swing/plaf/windows/icons/File.gif")));
		toolBar.add(fileNewBtn);

		JScrollPane scrollPane = new JScrollPane();
		frame.getContentPane().add(scrollPane, BorderLayout.CENTER);

		editArea = new JTextArea();
		editArea.getDocument().addDocumentListener(new DocumentListener() {
			private void setStatus(DocumentEvent e) {
				statusWordNumber.setText(e.getDocument().getLength() + " 字");

				// 在状态栏显示光标位置
				try {
					int c = editArea.getCaretPosition();
					int line = editArea.getLineOfOffset(c) + 1; // 行
					int col = c - editArea.getLineStartOffset(line - 1) + 1; // 列
					statusCoordinate.setText(line + "," + col);
				} catch (BadLocationException e1) {
					e1.printStackTrace();
				}
			}

			@Override
			public void removeUpdate(DocumentEvent e) {
				setStatus(e);
			}

			@Override
			public void insertUpdate(DocumentEvent e) {
				setStatus(e);
			}

			@Override
			public void changedUpdate(DocumentEvent e) {
				setStatus(e);
			}
		});
		editArea.addCaretListener(new CaretListener() {

			@Override
			public void caretUpdate(CaretEvent e) {
				Thread.currentThread().getStackTrace()[2].getMethodName();
			}
		});
		scrollPane.setViewportView(editArea);
	}

}
